//index.js
//获取应用实例
const app = getApp();
var promise = require('../../untils/es6Promise.js');
Page({
  data: {
    addName:'',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular: true,
    indicatorActiveColor: '#fff',
    url: getApp().globalData.https_url,
    ypy_url: getApp().globalData.ypy_url,
    // 轮播图
    imgUrls:'',
    navArrs: [
      { image: '../../images/index/aiqingditu.png', text: '爱情地图' },
      { image: '../../images/index/meirisupei.png', text: '每日速配' },
      { image: '../../images/index/aiqinghuketing.png', text: '爱情会客厅' },
      { image: '../../images/index/meiriqiandao.png', text: '每日签到' },
      // { image: '../../images/index/meiriqiandao.png', text: '爱心商家 ' },
      // { image: '../../images/index/meiriqiandao.png', text: '活动基地' },
      // { image: '../../images/index/meiriqiandao.png', text: '活动图片' },
      // { image: '../../images/index/meiriqiandao.png', text: '各地动态' },
    ],
    typeArrs: [
      { imageno: '../../images/index/renqino.png', imageyes: '../../images/index/tuijianyes.png', text: '推荐', state: 1, left: '2%' },
      { imageno: '../../images/index/renqino.png', imageyes: '../../images/index/tuijianyes.png', text: '人气', state: 0, left: '22%' },
      { imageno: '../../images/index/zuixinno.png', imageyes: '../../images/index/tuijianyes.png', text: '最新', state: 0, left: '42%' },
      { imageno: '../../images/index/nanshenno.png', imageyes: '../../images/index/tuijianyes.png', text: '男神', state: 0, left: '62%' },
      { imageno: '../../images/index/nvshenno.png', imageyes: '../../images/index/tuijianyes.png', text: '女神', state: 0, left: '80%' }
    ],
    // 记录上一个点击的类型
    thatType: "typeArrs[0].state",
    thisState:0,
    // 下划线的left
    lineContentLeft: '2%',
    // 图片列表
    listArr1:'',
    listArr2: '',
    listArr3: '',
    listArr4: '',
    listArr5: '',
    // 风景图片列表
    activityList:''
  },
  //事件处理函数
  onLoad:function(){  
  },
  onShow:function(){
    wx.showLoading({
      title: '数据加载中...',
      mask:true
    })
    var _this=this;
    var userid = app.globalData.userid;
    if(!userid){
      var openid=wx.getStorageSync('openid');
      if(!openid){
          app.wxLogin().then(function(v){//
            if(v){
                  app.ifLogin().then(function(){
                    _this.initPage();
              })
            }   
          })
      }else{
        app.getUserInfoSyn({ "openId": wx.getStorageSync('openid')}).then(function(){
              _this.initPage();
            })
      }
    }else{
       _this.initPage();
     }
  },
  initPage:function(){
   
  var _this = this;
    _this.setData({
      addName: wx.getStorageSync('addName')
    })

    var pro = new Promise(function (resolve,reject){

      _this.getMemberInfo();
      resolve();
    });
    pro.then(function (result){
  
      wx.request({//轮播
        url: getApp().globalData.https_url + 'wxPictures',
        method: "GET",
        dataType: "json",
        success: function (res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            _this.setData({
              imgUrls: res.data.obj,
            })
          }
        },
        fail:function(res){
         
        }
      });

      wx.request({//交友活动
        url: getApp().globalData.https_url + 'wxBlindList', 
        data: {
          'id': getApp().globalData.userid,
          'pagesize': 4,
          'code': wx.getStorageSync('addCode')
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
          if (res.data.code == 200) {
            _this.setData({
              activityList: res.data.pageitems,
            })
          }
        },
        fail: function (res) {
          
        }
      });
    });
      wx.getLocation({
        success: function (res) {
          console.log('成功获得用户定位信息');
          console.log(res);
          //暂时把地址限定为浙江
          wx.setStorageSync('addName', '浙江省');
          wx.setStorageSync('addCode', 33);
         
        }
      });
  },
  getMemberInfo:function(){
    var _this=this;
         wx.request({//推荐会员
        url: getApp().globalData.https_url + 'wxMemberList', 
        data: {
          'id': getApp().globalData.userid,
          'xgmate.type': 3,
          'xgmember.sex': '',
          'pagesize': 9,
          'code':wx.getStorageSync('addCode')
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
          if (res.data.code == 200) {
            _this.setData({
              listArr1: res.data.pageitems,
            })
          }
        }
      })
      wx.request({//人气会员
        url: getApp().globalData.https_url + 'wxMemberList',
        data: {
          'id': getApp().globalData.userid,
          'xgmate.type': 2,
          'xgmember.sex': '',
          'pagesize': 9,
          'code':wx.getStorageSync('addCode')
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
          if (res.data.code == 200) {
            _this.setData({
              listArr2: res.data.pageitems,
            })
          }
        }
      })
      wx.request({//最新会员
        url: getApp().globalData.https_url + 'wxMemberList', 
        data: {
          'id': getApp().globalData.userid,
          'xgmate.type': 1,
          'xgmember.sex': '',
          'pagesize': 9,
          'code':wx.getStorageSync('addCode')
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
          if (res.data.code == 200) {
            _this.setData({
              listArr3: res.data.pageitems,
            })
          }
        }
      })
      wx.request({//男会员
        url: getApp().globalData.https_url + 'wxMemberList', 
        data: {
          'id': getApp().globalData.userid,
          'xgmember.sex': 402,
          'pagesize': 9,
          'code':wx.getStorageSync('addCode')
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
          if (res.data.code == 200) {
            _this.setData({
              listArr4: res.data.pageitems,
            })
          }
        }
      })
      wx.request({
        url: getApp().globalData.https_url + 'wxMemberList',
        data: {
          'id': getApp().globalData.userid,
          'xgmember.sex': 403,
          'pagesize': 9,
          'code':wx.getStorageSync('addCode')
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
          if (res.data.code == 200) {
            _this.setData({
              listArr5: res.data.pageitems,
            })
          }
        },
        fail:function(){
            
        },
      })
  },
  goSomePage: function (e) {
    if (e.currentTarget.dataset.index == 0) {
      wx.navigateTo({
        url: '../../pages/loveMap/loveMap'
      })
    } else if (e.currentTarget.dataset.index == 1) {
      wx.navigateTo({
        url: '../../pages/speedDating/speedDating',
      });

    } else if (e.currentTarget.dataset.index == 2) {
      wx.navigateTo({
        url: '../../pages/loveReceptionRoom/loveReceptionRoom',
      })
    } else if (e.currentTarget.dataset.index == 3) {
      wx.navigateTo({
        url: '../../pages/dailyAttendance/dailyAttendance',
      })
    } else if (e.currentTarget.dataset.index == 5) {
      wx.navigateTo({
        url: '../../pages/activityBase/activityBase',
      })
    } else if (e.currentTarget.dataset.index == 6) {
      wx.navigateTo({
        url: '../../pages/activityPictures/activityPictures',
      })
    } else if (e.currentTarget.dataset.index == 7) {
      wx.navigateTo({
        url: '../../pages/activityNews/activityNews',
      })
    }
  },
  tapType: function (e) {
    var _this = this;
    // 修改点击的类型的状态
    var thisState = "typeArrs[" + e.currentTarget.dataset.index + "].state";
    var thatState = _this.data.thatType;
    
    if (thatState != thisState) {
      _this.setData({
        [thisState]: 1,
        [thatState]: 0,
        thatType: thisState,
        lineContentLeft: _this.data.typeArrs[e.currentTarget.dataset.index].left
      })      
    }
    _this.setData({
      thisState: e.currentTarget.dataset.index,
    })  
    
  },
  goChooseCityPage: function () {
    wx.navigateTo({
      url: '../../pages/chooseCity/chooseCity'
    })
  },
  Youzhihuiyuan: function () {
    wx.navigateTo({
      url: '../../pages/makeFriends/makeFriends'
    })
  },
  goActivityList: function () {
    wx.switchTab({
      url: '../../pages/activityList/activityList'
    })
  },
  goActivityNews: function () {
    wx.navigateTo({
      url: '../../pages/activityNews/activityNews'
    })
  },
  goActivityPictures: function () {
    wx.navigateTo({
      url: '../../pages/activityPictures/activityPictures'
    })
  },
  goSomeimage: function () {
    wx.navigateTo({
      url: '../../pages/Member_details/Member_details'
    })
  },
  activityDetails:function(e){
    var typeid = e.currentTarget.dataset.typeid;
    wx.navigateTo({
      url: '../../pages/activityDetails/activityDetails?typeid=' +typeid,
    })
  },
  goMemberDetails:function(e){//点击图片进入会员详情页面
    var memberid = app.getBindDataSet(e).memberid;
    wx.navigateTo({
      url: '../../pages/memberDetails/memberDetails?userid=' + memberid,
    })
  },
  goMemberList:function(e){
    wx.switchTab({//tobar元素无法使用 wx.navigateTo 跳转
      url:'../../pages/makeFriends/makeFriends',
    });
  },
  goActivityDetails:function(e){//跳转到活动图片列表
    var id = app.getBindDataSet(e).typeid;
    wx.navigateTo({
      url: '../../pages/activityDetails/activityDetails?typeid=' + id,
    });
  },
  goActivitys:function(){
    wx.switchTab({
      url: '../../pages/activityList/activityList',
    });
  },
  onTabItemTap:function(i){
    console.log(i);
  }
})
